package com.atguigu.gulimall.order.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

/**
 * @create: 2024/9/7
 * @author: gyhe10
 * @description:
 **/

// @EnableRedisHttpSession// 启用基于Redis的会话存储，启动类上已添加，这里不需要配置
@Configuration
public class GulimallSessionConfig {

    // 配置自定义的CookieSerializer
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
        // 设置cookie域名为gulimall.com，并更改cookie名称为GULIMALLSESSION
        defaultCookieSerializer.setDomainName("gulimall.com");
        defaultCookieSerializer.setCookieName("GULIMALLSESSION");
        return defaultCookieSerializer;
    }

    // 提供一个JSON序列化器GenericJackson2JsonRedisSerializer用于序列化会话数据到Redis
    @Bean
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }
}
